home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Internet / WinHTTrack 3.41-2 / httrack-3.41-2.exe / {app} / src / htscoremain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-03  |  88.9 KB  |  2,284 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: opt->c subroutines:                                 */
  34. /*       main routine (first called)                            */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscoremain.h"
  42.  
  43. #include "htsglobal.h"
  44. #include "htscore.h"
  45. #include "htsdefines.h"
  46. #include "htsalias.h"
  47. #include "htswrap.h"
  48. #include "htsmodules.h"
  49. #include "htszlib.h"
  50.  
  51. #include <ctype.h>
  52. #if USE_BEGINTHREAD
  53. #ifdef _WIN32
  54. #include <process.h>
  55. #endif
  56. #endif
  57. #ifdef _WIN32
  58. #else
  59. #ifndef HTS_DO_NOT_USE_UID
  60. /* setuid */
  61. #include <pwd.h>
  62. #ifdef HAVE_UNISTD_H
  63. #include <unistd.h>
  64. #endif
  65. #endif
  66. #endif
  67.  
  68. /* Resolver */
  69. extern int IPV6_resolver;
  70.  
  71.  
  72. // Add a command in the argc/argv
  73. #define cmdl_add(token,argc,argv,buff,ptr) \
  74.   argv[argc]=(buff+ptr); \
  75.   strcpybuff(argv[argc],token); \
  76.   ptr += (int) (strlen(argv[argc])+2); \
  77.   argc++
  78.  
  79. // Insert a command in the argc/argv
  80. #define cmdl_ins(token,argc,argv,buff,ptr) \
  81.   { \
  82.   int i; \
  83.   for(i=argc;i>0;i--)\
  84.   argv[i]=argv[i-1];\
  85.   } \
  86.   argv[0]=(buff+ptr); \
  87.   strcpybuff(argv[0],token); \
  88.   ptr += (int) (strlen(argv[0])+2); \
  89.   argc++
  90.  
  91. #define htsmain_free() do { if (url != NULL) { free(url); } } while(0)
  92.  
  93. #define ensureUrlCapacity(url, urlsize, size) do { \
  94.   if (urlsize < size || url == NULL) { \
  95.     urlsize = size; \
  96.     if (url == NULL) { \
  97.       url = (char*) malloct(urlsize); \
  98.       if (url != NULL) url[0]='\0'; \
  99.     } else { \
  100.       url = (char*) realloct(url, urlsize); \
  101.     } \
  102.     if (url == NULL) { \
  103.       HTS_PANIC_PRINTF("* memory exhausted"); \
  104.       htsmain_free(); \
  105.       return -1; \
  106.     } \
  107.   } \
  108. } while(0)
  109.  
  110. HTSEXT_API int hts_main(int argc, char **argv)
  111. {
  112.   httrackp *opt = hts_create_opt();
  113.     int ret = hts_main2(argc, argv, opt);
  114.     hts_free_opt(opt);
  115.     return ret;
  116. }
  117.  
  118. // Main, rΘcupΦre les paramΦtres et appelle le robot
  119. HTSEXT_API int hts_main2(int argc, char **argv, httrackp *opt) {
  120.   char** x_argv=NULL;     // Patch pour argv et argc: en cas de rΘcupΘration de ligne de commande
  121.   char* x_argvblk=NULL;   // (reprise ou update)
  122.   int   x_ptr=0;          // offset
  123.   //
  124.   int argv_url=-1;           // ==0 : utiliser cache et doit.log
  125.   char* argv_firsturl=NULL;  // utilisΘ pour nommage par dΘfaut
  126.   char* url = NULL;          // URLS sΘparΘes par un espace
  127.   int   url_sz = 65535;
  128.   //char url[65536];         // URLS sΘparΘes par un espace
  129.   // the parametres
  130.   int httrack_logmode=3;   // ONE log file
  131.   int recuperer=0;         // rΘcupΘrer un plantage (n'arrive jamais, α supprimer)
  132. #ifndef _WIN32
  133. #ifndef HTS_DO_NOT_USE_UID
  134.   int switch_uid=-1,switch_gid=-1;      /* setuid/setgid */
  135. #endif
  136.   int switch_chroot=0;                  /* chroot ? */
  137. #endif
  138.   //
  139.   ensureUrlCapacity(url, url_sz, 65536);
  140.  
  141.     // Create options
  142.     _DEBUG_HEAD=0;            // pas de debuggage en tΩtes
  143.   
  144.   /* Init root dir */
  145.   hts_rootdir(argv[0]);
  146.  
  147. #ifdef _WIN32
  148. #else
  149.   /* Terminal is a tty, may ask questions and display funny information */
  150.   if (isatty(1)) {
  151.     opt->quiet=0;
  152.     opt->verbosedisplay=1;
  153.   }
  154.   /* Not a tty, no stdin input or funny output! */
  155.   else {
  156.     opt->quiet=1;
  157.     opt->verbosedisplay=0;
  158.   }
  159. #endif
  160.  
  161.   // Binary program path?
  162. #ifndef HTS_HTTRACKDIR
  163.   {
  164.       char catbuff[CATBUFF_SIZE];
  165.     char* path=fslash(catbuff,argv[0]);
  166.     char* a;
  167.     if ((a=strrchr(path,'/'))) {
  168.       StringCopyN(opt->path_bin,argv[0],a - path);
  169.     }
  170.   }
  171. #else
  172.   StringCopy(opt->path_bin, HTS_HTTRACKDIR);
  173. #endif
  174.  
  175.   /* filter CR, LF, TAB.. */
  176.   {
  177.     int na;
  178.     for(na=1;na<argc;na++) {
  179.       char* a;
  180.       while( (a=strchr(argv[na],'\x0d')) ) *a=' ';
  181.       while( (a=strchr(argv[na],'\x0a')) ) *a=' ';
  182.       while( (a=strchr(argv[na],9)) )      *a=' ';
  183.       /* equivalent to "empty parameter" */
  184.       if ((strcmp(argv[na],HTS_NOPARAM)==0) || (strcmp(argv[na],HTS_NOPARAM2)==0))        // (none)
  185.         strcpybuff(argv[na],"\"\"");
  186.       if (strncmp(argv[na],"-&",2)==0)
  187.         argv[na][1]='%';
  188.     }
  189.   }
  190.  
  191.   /* create x_argvblk buffer for transformed command line */
  192.   {
  193.     int current_size=0;
  194.     int size;
  195.     int na;
  196.     for(na=0;na<argc;na++)
  197.       current_size += (int) (strlen(argv[na]) + 1);
  198.     if ((size=fsize("config"))>0)
  199.       current_size += size;
  200.     x_argvblk=(char*) malloct(current_size+32768);
  201.     if (x_argvblk == NULL) {
  202.       HTS_PANIC_PRINTF("Error, not enough memory");
  203.       htsmain_free();
  204.       return -1;
  205.     }
  206.     x_argvblk[0]='\0';
  207.     x_ptr=0;
  208.  
  209.     /* Create argv */
  210.     x_argv = (char**) malloct(sizeof(char*) * ( argc + 1024 ));
  211.   }
  212.  
  213.   /* Create new argc/argv, replace alias, count URLs, treat -h, -q, -i */
  214.   {
  215.     char BIGSTK _tmp_argv[2][HTS_CDLMAXSIZE];
  216.     char BIGSTK tmp_error[HTS_CDLMAXSIZE];
  217.     char* tmp_argv[2];
  218.     int tmp_argc;
  219.     int x_argc=0;
  220.     int na;
  221.     tmp_argv[0]=_tmp_argv[0];
  222.     tmp_argv[1]=_tmp_argv[1];
  223.     //
  224.     argv_url=0;       /* pour comptage */
  225.     //
  226.     cmdl_add(argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  227.     na=1;             /* commencer aprΦs nom_prg */
  228.     while(na<argc) {
  229.       int result=1;
  230.       tmp_argv[0][0]=tmp_argv[1][0]='\0';
  231.  
  232.       /* VΘrifier argv[] non vide */
  233.       if (strnotempty(argv[na])) {
  234.         
  235.         /* VΘrifier Commande (alias) */
  236.         result=optalias_check(argc,(const char * const *)argv,na,
  237.           &tmp_argc,(char**)tmp_argv,tmp_error);
  238.         if (!result) {
  239.           HTS_PANIC_PRINTF(tmp_error);
  240.           htsmain_free();
  241.           return -1;
  242.         }
  243.         
  244.         /* Copier */
  245.         cmdl_add(tmp_argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  246.         if (tmp_argc > 1) {
  247.           cmdl_add(tmp_argv[1],x_argc,x_argv,x_argvblk,x_ptr);
  248.         }
  249.         
  250.         /* Compter URLs et dΘtecter -i,-q.. */
  251.         if (tmp_argc == 1) {           /* pas -P & co */
  252.           if (!cmdl_opt(tmp_argv[0])) {   /* pas -c0 & co */
  253.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  254.             //if (argv_url>=0) {
  255.             argv_url++;
  256.             if (!argv_firsturl)
  257.               argv_firsturl=x_argv[x_argc-1];
  258.             //}
  259.           } else {
  260.             if (strcmp(tmp_argv[0],"-h")==0) {
  261.               help(argv[0],!opt->quiet);
  262.               htsmain_free();
  263.               return 0;
  264.             } else {
  265.               if (strncmp(tmp_argv[0],"--",2)) {   /* pas */
  266.                 if ((strchr(tmp_argv[0],'q')!=NULL))
  267.                   opt->quiet=1;    // ne pas poser de questions! (nohup par exemple)
  268.                 if ((strchr(tmp_argv[0],'i')!=NULL)) {  // doit.log!
  269.                   argv_url=-1;        /* forcer */
  270.                   opt->quiet=1;
  271.                 }
  272.               } else if (strcmp(tmp_argv[0] + 2,"quiet") == 0) {
  273.                 opt->quiet=1;    // ne pas poser de questions! (nohup par exemple)
  274.               } else if (strcmp(tmp_argv[0] + 2,"continue") == 0) {
  275.                 argv_url=-1;        /* forcer */
  276.                 opt->quiet=1;
  277.               }
  278.             }
  279.           }
  280.         } else if (tmp_argc == 2) {
  281.           if ((strcmp(tmp_argv[0],"-%L")==0)) {  // liste d'URLs
  282.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  283.             //if (argv_url>=0)
  284.             argv_url++;        /* forcer */
  285.           }
  286.         }
  287.       }
  288.  
  289.       na+=result;
  290.     }
  291.     if (argv_url<0)
  292.       argv_url=0;
  293.  
  294.     /* Nouveaux argc et argv */
  295.     argv=x_argv;
  296.     argc=x_argc;
  297.   }
  298.  
  299.   // Option O and includerc
  300.   { 
  301.     int loops=0;
  302.     while (loops<2) {
  303.       char* com;
  304.       int na;
  305.       
  306.       for(na=1;na<argc;na++) {
  307.         
  308.         if (argv[na][0]=='"') {
  309.           char BIGSTK tempo[HTS_CDLMAXSIZE];
  310.           strcpybuff(tempo,argv[na]+1);
  311.           if (tempo[strlen(tempo)-1]!='"') {
  312.             char BIGSTK s[HTS_CDLMAXSIZE];
  313.             sprintf(s,"Missing quote in %s",argv[na]);
  314.             HTS_PANIC_PRINTF(s);
  315.             htsmain_free();
  316.             return -1;
  317.           }
  318.           tempo[strlen(tempo)-1]='\0';
  319.           strcpybuff(argv[na],tempo);
  320.         }
  321.         
  322.         if (cmdl_opt(argv[na])) { // option
  323.           com=argv[na]+1;
  324.           
  325.           while(*com) {
  326.             switch(*com) {
  327.             case 'O':    // output path
  328.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  329.                 HTS_PANIC_PRINTF("Option O needs to be followed by a blank space, and a path (or path,path)");
  330.                 printf("Example: -O /binary/\n");
  331.                 printf("Example: -O /binary/,/log/\n");
  332.                 htsmain_free();
  333.                 return -1;
  334.               } else {
  335.                                 int i, j;
  336.                                 int inQuote;
  337.                                 String *path;
  338.                                 int noDbl = 0;
  339.                                 if (com[1] == '1') {            /* only 1 arg */
  340.                                     com++;
  341.                                     noDbl = 1;
  342.                                 }
  343.                 na++;
  344.                                 StringClear(opt->path_html);
  345.                 StringClear(opt->path_log);
  346.                                 for(i = 0, j = 0, inQuote = 0, path = &opt->path_html ; argv[na][i] != 0 ; i++) {
  347.                                     if (argv[na][i] == '"') {
  348.                                         if (inQuote)
  349.                                             inQuote = 0;
  350.                                         else
  351.                                             inQuote = 1;
  352.                                     } else if (!inQuote && !noDbl && argv[na][i] == ',') {
  353.                     //StringAddchar(path, '\0');
  354.                                         j = 0;
  355.                                         path = &opt->path_log;
  356.                                     } else {
  357.                     StringAddchar(*path, argv[na][i]);
  358.                                         //path[j++] = argv[na][i];
  359.                                     }
  360.                                 }
  361.                                 //path[j++] = '\0';
  362.                                 if (StringLength(opt->path_log) == 0) {
  363.                                     StringCopyS(opt->path_log, opt->path_html);
  364.                                 }
  365.  
  366.                 check_path(&opt->path_log, argv_firsturl);
  367.                 if (check_path(&opt->path_html, argv_firsturl)) {
  368.                   opt->dir_topindex=1;     // rebuilt top index
  369.                 }
  370.                 
  371.                 //printf("-->%s\n%s\n",StringBuff(opt->path_html),StringBuff(opt->path_log));                
  372.               }
  373.               break;
  374.             }  // switch
  375.             com++;    
  376.           }  // while
  377.           
  378.         }  // arg
  379.         
  380.       }  // for
  381.      
  382.          /* if doit.log exists, or if new URL(s) defined, 
  383.       then DO NOT load standard config files */
  384.       /* (config files are added in doit.log) */
  385. #if DEBUG_STEPS
  386.       printf("Loading httrackrc/doit.log\n");
  387. #endif
  388.       /* recreate a doit.log (no old doit.log or new URLs (and parameters)) */
  389.       if ((strnotempty(StringBuff(opt->path_log))) || (strnotempty(StringBuff(opt->path_html))))
  390.         loops++;      // do not loop once again and do not include rc file (O option exists)
  391.       else {
  392.         if ( (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log"))) || (argv_url>0) ) {
  393.           if (!optinclude_file(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr))
  394.             if (!optinclude_file(HTS_HTTRACKRC,&argc,argv,x_argvblk,&x_ptr)) {
  395.               if (!optinclude_file(fconcat(OPT_GET_BUFF(opt), hts_gethome(),"/"HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr)) {
  396. #ifdef HTS_HTTRACKCNF
  397.                 optinclude_file(HTS_HTTRACKCNF,&argc,argv,x_argvblk,&x_ptr);
  398. #endif
  399.               }
  400.             }
  401.         } else
  402.           loops++;      // do not loop once again
  403.       }
  404.  
  405.       loops++;
  406.    } // while
  407.  
  408.   }  // traiter -O
  409.  
  410.   /* load doit.log and insert in current command line */
  411.   if ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log")) && (argv_url<=0) ) {
  412.     FILE* fp=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log"),"rb");
  413.     if (fp) {
  414.       int insert_after=1;     /* insΘrer aprΦs nom au dΘbut */
  415.       //
  416.       char BIGSTK buff[8192];
  417.       char *p,*lastp;
  418.       linput(fp,buff,8000);
  419.       fclose(fp); fp=NULL;
  420.       p=buff;
  421.       do {
  422.         int insert_after_argc;
  423.         // read next
  424.         lastp=p;
  425.         if (p) {
  426.           p=next_token(p,1);
  427.           if (p) {
  428.             *p=0;    // null
  429.             p++;
  430.           }
  431.         }
  432.  
  433.         /* Insert parameters BUT so that they can be in the same order */
  434.         if (lastp) {
  435.           if (strnotempty(lastp)) {
  436.             insert_after_argc=argc-insert_after;
  437.             cmdl_ins(lastp,insert_after_argc,(argv+insert_after),x_argvblk,x_ptr);
  438.             argc=insert_after_argc+insert_after;
  439.             insert_after++;
  440.           }
  441.         }
  442.       } while(lastp!=NULL);
  443.       //fclose(fp);
  444.     }
  445.   }
  446.  
  447.  
  448.   // Existence d'un cache - pas de new mais un old.. renommer
  449. #if DEBUG_STEPS
  450.   printf("Checking cache\n");
  451. #endif
  452.   if (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"))) {
  453.     if ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")) ) {
  454.       rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"));
  455.     }
  456.   } else if ( (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"))) || (!fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"))) ) {
  457.     if ( (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"))) && (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"))) ) {
  458.       remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"));
  459.       remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));
  460.       //remove(fconcat(StringBuff(opt->path_log),"hts-cache/new.lst"));
  461.       rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"));
  462.       rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));
  463.       //rename(fconcat(StringBuff(opt->path_log),"hts-cache/old.lst"),fconcat(StringBuff(opt->path_log),"hts-cache/new.lst"));
  464.     }
  465.   }
  466.  
  467.   /* Interrupted mirror detected */
  468.   if (!opt->quiet) {
  469.     if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"))) {
  470.       /* Old cache */
  471.       if ( (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"))) && (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"))) ) {
  472.         if (opt->log != NULL) {
  473.           fprintf(opt->log,"Warning!\n");
  474.           fprintf(opt->log,"An aborted mirror has been detected!\nThe current temporary cache is required for any update operation and only contains data downloaded during the last aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files\n");
  475.           fprintf(opt->log,"Please restart HTTrack with --continue (-iC1) option to override this message!\n");
  476.         }
  477.         exit(0);
  478.       }
  479.     }
  480.   }
  481.     
  482.   // remplacer "macros" comme --spider
  483.   // permet de lancer httrack sans a avoir α se rappeler de syntaxes comme p0C0I0Qc32 ..
  484. #if DEBUG_STEPS
  485.   printf("Checking last macros\n");
  486. #endif
  487.   {
  488.     int i;
  489.     for(i=0;i<argc;i++) {
  490. #if DEBUG_STEPS
  491.       printf("Checking #%d:\n",argv[i]);
  492.       printf("%s\n",argv[i]);
  493. #endif
  494.       if (argv[i][0]=='-') {
  495.         if (argv[i][1]=='-') {  // --xxx
  496.           if ((strfield2(argv[i]+2,"clean")) || (strfield2(argv[i]+2,"tide"))) {  // nettoyer
  497.             strcpybuff(argv[i]+1,"");
  498.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt")))
  499.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt"));
  500.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt")))
  501.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt"));
  502.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html")))
  503.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html"));
  504.             /* */
  505.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip")))
  506.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"));
  507.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")))
  508.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"));
  509.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")))
  510.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"));
  511.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")))
  512.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));
  513.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat")))
  514.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"));
  515.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx")))
  516.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"));
  517.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.lst")))
  518.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.lst"));
  519.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.lst")))
  520.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.lst"));
  521.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.txt")))
  522.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.txt"));
  523.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.txt")))
  524.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.txt"));
  525.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log")))
  526.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log"));
  527.             if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock")))
  528.               remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"));
  529.             rmdir(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache"));
  530.             //
  531.           } else if (strfield2(argv[i]+2,"catchurl")) {      // capture d'URL via proxy temporaire!
  532.             argv_url=1;     // forcer a passer les parametres
  533.             strcpybuff(argv[i]+1,"#P");
  534.             //
  535.           } else if (strfield2(argv[i]+2,"updatehttrack")) {
  536. #ifdef _WIN32
  537.             char s[HTS_CDLMAXSIZE];
  538.             sprintf(s,"%s not available in this version",argv[i]);
  539.             HTS_PANIC_PRINTF(s);
  540.             htsmain_free();
  541.             return -1;
  542. #else
  543. #if 0
  544.             char _args[8][256];
  545.             char *args[8];
  546.             
  547.             printf("Cheking for updates...\n");
  548.             strcpybuff(_args[0],argv[0]);
  549.             strcpybuff(_args[1],"--get");
  550.             sprintf(_args[2],HTS_UPDATE_WEBSITE,0,"");
  551.             strcpybuff(_args[3],"--quickinfo");
  552.             args[0]=_args[0];
  553.             args[1]=_args[1];
  554.             args[2]=_args[2];
  555.             args[3]=_args[3];
  556.             args[4]=NULL;
  557.             if (execvp(args[0],args)==-1) {
  558.             }
  559. #endif
  560. #endif
  561.           }
  562.           //
  563.           else {
  564.             char s[HTS_CDLMAXSIZE];
  565.             sprintf(s,"%s not recognized",argv[i]);
  566.             HTS_PANIC_PRINTF(s);
  567.             htsmain_free();
  568.             return -1;
  569.           }
  570.  
  571.         } 
  572.       }
  573.     }
  574.   }
  575.  
  576.   // Compter urls/jokers
  577.   /*
  578.   if (argv_url<=0) { 
  579.     int na;
  580.     argv_url=0;
  581.     for(na=1;na<argc;na++) {
  582.       if ( (strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0) ) {
  583.         na++;    // sauter nom de proxy
  584.       } else if (!cmdl_opt(argv[na])) { 
  585.         argv_url++;   // un de plus       
  586.       } else if (strcmp(argv[na],"-h")==0) {
  587.         help(argv[0],!opt->quiet);
  588.         htsmain_free();
  589.         return 0;
  590.       } else {
  591.         if ((strchr(argv[na],'q')!=NULL))
  592.           opt->quiet=1;    // ne pas poser de questions! (nohup par exemple)
  593.         if ((strchr(argv[na],'i')!=NULL)) {  // doit.log!
  594.           argv_url=0;
  595.           na=argc;
  596.         }
  597.       }
  598.     }
  599.   }  
  600.   */
  601.  
  602.   // Ici on ajoute les arguments qui ont ΘtΘ appelΘs avant au cas o∙ on rΘcupΦre une session
  603.   // Exemple: httrack www.truc.fr -L0 puis ^C puis httrack sans URL : ajouter URL prΘcΘdente
  604.   /*
  605.   if (argv_url==0) {
  606.     //if ((fexist(fconcat(StringBuff(opt->path_log),"hts-cache/new.dat"))) && (fexist(fconcat(StringBuff(opt->path_log),"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  607.     if (fexist(fconcat(StringBuff(opt->path_log),"hts-cache/doit.log"))) {    // un cache est prΘsent
  608.       
  609.       x_argvblk=(char*) calloct(32768,1);
  610.       
  611.       if (x_argvblk!=NULL) {
  612.         FILE* fp;
  613.         int x_argc;
  614.         
  615.         //strcpybuff(x_argvblk,"httrack ");
  616.         fp=fopen(fconcat(StringBuff(opt->path_log),"hts-cache/doit.log"),"rb");
  617.         if (fp) {
  618.           linput(fp,x_argvblk+strlen(x_argvblk),8192);
  619.           fclose(fp); fp=NULL;
  620.         }
  621.         
  622.         // calculer arguments selon derniers arguments
  623.         x_argv[0]=argv[0];
  624.         x_argc=1;
  625.         {
  626.           char* p=x_argvblk;
  627.           do {
  628.             x_argv[x_argc++]=p;
  629.             //p=strstr(p," ");
  630.             // exemple de chaine: "echo \"test\"" c:\a "\$0"
  631.             p=next_token(p,1);    // prochain token
  632.             if (p) {
  633.               *p=0;    // octet nul (tableau)
  634.               p++;
  635.             }            
  636.           } while(p!=NULL);
  637.         }
  638.         // recopier arguments actuels (pointeurs uniquement)
  639.         {
  640.           int na;
  641.           for(na=1;na<argc;na++) {
  642.             if (strcmp(argv[na],"-O") != 0)    // SAUF le path!
  643.               x_argv[x_argc++]=argv[na];
  644.             else
  645.               na++;
  646.           }
  647.         }
  648.         argc=x_argc;      // nouvel argc
  649.         argv=x_argv;      // nouvel argv
  650.       }
  651.       
  652.       
  653.     }
  654.     //}
  655.   }
  656.   */
  657.   
  658.   // VΘrifier quiet
  659.   /*
  660.   { 
  661.     int na;    
  662.     for(na=1;na<argc;na++) {
  663.       if (!cmdl_opt(argv[na])) { 
  664.         if ((strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0))
  665.           na++;    // sauter nom de proxy
  666.       } else {
  667.         if ((strchr(argv[na],'q')!=NULL) || (strchr(argv[na],'i')!=NULL))
  668.           opt->quiet=1;    // ne pas poser de questions! (nohup par exemple)
  669.       }
  670.     }
  671.   }
  672.   */
  673.  
  674.   // Pas d'URL
  675. #if DEBUG_STEPS
  676.   printf("Checking URLs\n");
  677. #endif
  678.   if (argv_url==0) {
  679.     // PrΘsence d'un cache, que faire?..
  680.     if (
  681.       ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip")) )
  682.       ||
  683.       ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")) && fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")) )
  684.       ) {  // il existe dΘja un cache prΘcΘdent.. renommer
  685.       if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log"))) {    // un cache est prΘsent
  686.         if (x_argvblk!=NULL) {
  687.           int m;        
  688.           // Θtablir mode - mode cache: 1 (cache valide) 2 (cache α vΘrifier)
  689.           if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"))) {    // cache prioritaire
  690.             m=1;
  691.             recuperer=1;
  692.           } else {
  693.             m=2;
  694.           }
  695.           opt->cache=m;
  696.           
  697.           if (opt->quiet==0) {  // sinon on continue automatiquement
  698.             HT_REQUEST_START;
  699.             HT_PRINT("A cache (hts-cache/) has been found in the directory ");
  700.             HT_PRINT(StringBuff(opt->path_log));
  701.             HT_PRINT(LF);
  702.             if (m==1) {
  703.               HT_PRINT("That means that a transfer has been aborted"LF);
  704.               HT_PRINT("OK to Continue ");
  705.             } else {
  706.               HT_PRINT("That means you can update faster the remote site(s)"LF);
  707.               HT_PRINT("OK to Update ");
  708.             }
  709.             HT_PRINT("httrack "); HT_PRINT(x_argvblk); HT_PRINT("?"LF);
  710.             HT_REQUEST_END;
  711.             if (!ask_continue(opt)) { 
  712.               htsmain_free();
  713.               return 0;
  714.             }
  715.           }
  716.           
  717.         } else {
  718.           HTS_PANIC_PRINTF("Error, not enough memory");
  719.           htsmain_free();
  720.           return -1;
  721.         }
  722.       } else { // log existe pas
  723.         HTS_PANIC_PRINTF("A cache has been found, but no command line");
  724.         printf("Please launch httrack with proper parameters to reuse the cache\n");
  725.         htsmain_free();
  726.         return -1;
  727.       }
  728.       
  729.     } else {    // aucune URL dΘfinie et pas de cache
  730.       if (argc > 1 && strcmp(argv[0], "-#h") == 0) {
  731.         printf("HTTrack version "HTTRACK_VERSION"%s\n", hts_get_version_info(opt));
  732.         exit(0);
  733.       }
  734.       if (opt->quiet) {
  735.         help(argv[0],!opt->quiet);
  736.         htsmain_free();
  737.         return -1;
  738.       } else {
  739.         help_wizard(opt);
  740.         htsmain_free();
  741.         return -1;
  742.       }
  743.       htsmain_free();
  744.       return 0;
  745.     }
  746.   } else {   // plus de 2 paramΦtres
  747.     // un fichier log existe?
  748.     if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"))) {  // fichier lock?
  749.       //char s[32];
  750.       
  751.       opt->cache=1;    // cache prioritaire
  752.       if (opt->quiet==0) {
  753.         if (
  754.           ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip")) )
  755.           ||
  756.           ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")) && fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")) )
  757.           ) {
  758.           HT_REQUEST_START;
  759.           HT_PRINT("There is a lock-file in the directory ");
  760.           HT_PRINT(StringBuff(opt->path_log));
  761.           HT_PRINT(LF"That means that a mirror has not been terminated"LF);
  762.           HT_PRINT("Be sure you call httrack with proper parameters"LF);
  763.           HT_PRINT("(The cache allows you to restart faster the transfer)"LF);
  764.           HT_REQUEST_END;
  765.           if (!ask_continue(opt)) {
  766.             htsmain_free();
  767.             return 0;
  768.           }
  769.         }
  770.       }
  771.     } else if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html),"index.html"))) {
  772.       //char s[32];
  773.       opt->cache=2;  // cache vient aprΦs test de validitΘ
  774.       if (opt->quiet==0) {
  775.         if (
  776.           ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip")) )
  777.           ||
  778.           ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")) && fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx")) )
  779.           ) {
  780.           HT_REQUEST_START;
  781.           HT_PRINT("There is an index.html and a hts-cache folder in the directory ");
  782.           HT_PRINT(StringBuff(opt->path_log));
  783.           HT_PRINT(LF"A site may have been mirrored here, that could mean that you want to update it"LF);
  784.           HT_PRINT("Be sure parameters are ok"LF);
  785.           HT_REQUEST_END;
  786.           if (!ask_continue(opt)) {
  787.             htsmain_free();
  788.             return 0;
  789.           }
  790.         } else {
  791.           HT_REQUEST_START;
  792.           HT_PRINT("There is an index.html in the directory ");
  793.           HT_PRINT(StringBuff(opt->path_log));
  794.           HT_PRINT(" but no cache"LF);
  795.           HT_PRINT("There is an index.html in the directory, but no cache"LF);
  796.           HT_PRINT("A site may have been mirrored here, and erased.."LF);
  797.           HT_PRINT("Be sure parameters are ok"LF);
  798.           HT_REQUEST_END;
  799.           if (!ask_continue(opt)) {
  800.             htsmain_free();
  801.             return 0;
  802.           }
  803.         }
  804.       }
  805.     }
  806.   }
  807.   
  808.   
  809.   // Treat parameters
  810.   // Traiter les paramΦtres
  811. #if DEBUG_STEPS
  812.   printf("Analyze parameters\n");
  813. #endif
  814.   { 
  815.     char* com;
  816.     int na;
  817.     
  818.     for(na=1;na<argc;na++) {
  819.  
  820.       if (argv[na][0]=='"') {
  821.         char BIGSTK tempo[HTS_CDLMAXSIZE];
  822.         strcpybuff(tempo,argv[na]+1);
  823.         if (tempo[strlen(tempo)-1]!='"') {
  824.           char s[HTS_CDLMAXSIZE];
  825.           sprintf(s,"Missing quote in %s",argv[na]);
  826.           HTS_PANIC_PRINTF(s);
  827.           htsmain_free();
  828.           return -1;
  829.         }
  830.         tempo[strlen(tempo)-1]='\0';
  831.         strcpybuff(argv[na],tempo);
  832.       }
  833.  
  834.       if (cmdl_opt(argv[na])) { // option
  835.         com=argv[na]+1;
  836.         
  837.         while(*com) {
  838.           switch(*com) {
  839.           case ' ': case 9: case '-': case '\0': break;
  840.             //
  841.           case 'h': 
  842.             help(argv[0],0); 
  843.             htsmain_free();
  844.             return 0;   // dΘja fait normalement
  845.             //
  846.           case 'g':    // rΘcupΘrer un (ou plusieurs) fichiers isolΘs
  847.             opt->wizard=2;             // le wizard on peut plus s'en passer..
  848.             //opt->wizard=0;             // pas de wizard
  849.             opt->cache=0;              // ni de cache
  850.             opt->makeindex=0;          // ni d'index
  851.             httrack_logmode=1;            // erreurs α l'Θcran
  852.             opt->savename_type=1003;   // mettre dans le rΘpertoire courant
  853.             opt->depth=0;              // ne pas explorer la page
  854.             opt->accept_cookie=0;      // pas de cookies
  855.             opt->robots=0;             // pas de robots
  856.             break;
  857.           case 'w': opt->wizard=2;    // wizard 'soft' (ne pose pas de questions)
  858.             opt->travel=0;
  859.             opt->seeker=1;
  860.             break;
  861.           case 'W': opt->wizard=1;    // Wizard-Help (pose des questions)
  862.             opt->travel=0;
  863.             opt->seeker=1;
  864.             break;
  865.           case 'r':                      // n'est plus le recurse get bestial mais wizard itou!
  866.             if (isdigit((unsigned char)*(com+1))) {
  867.               sscanf(com+1,"%d",&opt->depth);
  868.               while(isdigit((unsigned char)*(com+1))) com++;
  869.             } else opt->depth=3;
  870.             break;
  871. /*
  872.           case 'r': opt->wizard=0;
  873.             if (isdigit((unsigned char)*(com+1))) {
  874.               sscanf(com+1,"%d",&opt->depth);
  875.               while(isdigit((unsigned char)*(com+1))) com++;
  876.             } else opt->depth=3;
  877.             break;
  878. */
  879.             //
  880.             // note: les tests opt->depth sont pour Θviter de faire
  881.             // un miroir du web (:-O) accidentelement ;-)
  882.           case 'a': /*if (opt->depth==9999) opt->depth=3;*/
  883.             opt->travel=0+(opt->travel&256); break;
  884.           case 'd': /*if (opt->depth==9999) opt->depth=3;*/
  885.             opt->travel=1+(opt->travel&256); break;
  886.           case 'l': /*if (opt->depth==9999) opt->depth=3;*/
  887.             opt->travel=2+(opt->travel&256); break;
  888.           case 'e': /*if (opt->depth==9999) opt->depth=3;*/
  889.             opt->travel=7+(opt->travel&256); break;
  890.           case 't': opt->travel|=256; break;
  891.           case 'n': opt->nearlink=1; break;
  892.           case 'x': opt->external=1; break;
  893.             //
  894.           case 'U': opt->seeker=2; break;
  895.           case 'D': opt->seeker=1; break;
  896.           case 'S': opt->seeker=0; break;
  897.           case 'B': opt->seeker=3; break;
  898.             //
  899.           case 'Y': opt->mirror_first_page=1; break;
  900.             //
  901.           case 'q': case 'i': opt->quiet=1; break;
  902.             //
  903.           case 'Q': httrack_logmode=0; break;
  904.           case 'v': httrack_logmode=1; break;
  905.           case 'f': httrack_logmode=2; if (*(com+1)=='2') httrack_logmode=3; while(isdigit((unsigned char)*(com+1))) com++; break;
  906.             //
  907.           //case 'A': opt->urlmode=1; break;
  908.           //case 'R': opt->urlmode=2; break;
  909.           case 'K': opt->urlmode=0; 
  910.             if (isdigit((unsigned char)*(com+1))) {
  911.               sscanf(com+1,"%d",&opt->urlmode);
  912.               if (opt->urlmode == 0) {  // in fact K0 ==> K2
  913.                                            // and K ==> K0
  914.                 opt->urlmode=2;
  915.               }
  916.               while(isdigit((unsigned char)*(com+1))) com++; 
  917.             }
  918.             //if (*(com+1)=='0') { opt->urlmode=2; com++; } break;
  919.             //
  920.           case 'c':
  921.             if (isdigit((unsigned char)*(com+1))) {
  922.               sscanf(com+1,"%d",&opt->maxsoc);
  923.               while(isdigit((unsigned char)*(com+1))) com++;
  924.               opt->maxsoc=max(opt->maxsoc,1);     // FORCER A 1
  925.             } else opt->maxsoc=4;
  926.             
  927.             break;
  928.             //
  929.           case 'p': sscanf(com+1,"%d",&opt->getmode); while(isdigit((unsigned char)*(com+1))) com++; break;
  930.             //        
  931.           case 'G': sscanf(com+1,LLintP,&opt->fragment); while(isdigit((unsigned char)*(com+1))) com++; break;
  932.           case 'M': sscanf(com+1,LLintP,&opt->maxsite); while(isdigit((unsigned char)*(com+1))) com++; break;
  933.           case 'm': sscanf(com+1,LLintP,&opt->maxfile_nonhtml); while(isdigit((unsigned char)*(com+1))) com++; 
  934.             if (*(com+1)==',') {
  935.               com++;
  936.               sscanf(com+1,LLintP,&opt->maxfile_html); while(isdigit((unsigned char)*(com+1))) com++;
  937.             } else opt->maxfile_html=-1;
  938.             break;
  939.             //
  940.           case 'T': sscanf(com+1,"%d",&opt->timeout); while(isdigit((unsigned char)*(com+1))) com++; break;
  941.           case 'J': sscanf(com+1,"%d",&opt->rateout); while(isdigit((unsigned char)*(com+1))) com++; break;
  942.           case 'R': sscanf(com+1,"%d",&opt->retry); while(isdigit((unsigned char)*(com+1))) com++; break;
  943.           case 'E': sscanf(com+1,"%d",&opt->maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  944.           case 'H': sscanf(com+1,"%d",&opt->hostcontrol); while(isdigit((unsigned char)*(com+1))) com++; break;
  945.           case 'A': sscanf(com+1,"%d",&opt->maxrate); while(isdigit((unsigned char)*(com+1))) com++; break;
  946.  
  947.           case 'j':
  948.             opt->parsejava = HTSPARSE_DEFAULT;
  949.             if (isdigit((unsigned char)*(com+1))) {
  950.               sscanf(com+1,"%d",&opt->parsejava);
  951.               while(isdigit((unsigned char)*(com+1))) com++; 
  952.             }
  953.             break;
  954.             //
  955.           case 'I': opt->makeindex=1; if (*(com+1)=='0') { opt->makeindex=0; com++; } break;
  956.             //
  957.           case 'X': opt->delete_old=1; if (*(com+1)=='0') { opt->delete_old=0; com++; } break;
  958.             //
  959.           case 'b': sscanf(com+1,"%d",&opt->accept_cookie); while(isdigit((unsigned char)*(com+1))) com++; break;
  960.             //
  961.           case 'N':
  962.             if (strcmp(argv[na],"-N")==0) {    // Tout seul
  963.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {  // erreur
  964.                 HTS_PANIC_PRINTF("Option N needs a number, or needs to be followed by a blank space, and a string");
  965.                 printf("Example: -N4\n");
  966.                 htsmain_free();
  967.                 return -1;
  968.               } else {
  969.                 na++;
  970.                 if (strlen(argv[na])>=127) {
  971.                   HTS_PANIC_PRINTF("Userdef structure string too long");
  972.                   htsmain_free();
  973.                   return -1;
  974.                 }
  975.                 StringCopy(opt->savename_userdef, argv[na]);
  976.                 if (StringLength(opt->savename_userdef) > 0)
  977.                   opt->savename_type = -1;    // userdef!
  978.                 else
  979.                   opt->savename_type = 0;    // -N "" : par dΘfaut
  980.               }
  981.             } else {
  982.               sscanf(com+1,"%d",&opt->savename_type); while(isdigit((unsigned char)*(com+1))) com++;
  983.             }
  984.             break;
  985.           case 'L': 
  986.             {
  987.               sscanf(com+1,"%d",&opt->savename_83); 
  988.               switch(opt->savename_83) {
  989.               case 0:    // 8-3 (ISO9660 L1)
  990.                 opt->savename_83=1;
  991.                 break;
  992.               case 1:
  993.                 opt->savename_83=0;
  994.                 break;
  995.               default:    // 2 == ISO9660 (ISO9660 L2)
  996.                 opt->savename_83=2;
  997.                 break;
  998.               }
  999.               while(isdigit((unsigned char)*(com+1))) com++; 
  1000.             }
  1001.             break;
  1002.           case 's': 
  1003.             if (isdigit((unsigned char)*(com+1))) {
  1004.               sscanf(com+1,"%d",&opt->robots);
  1005.               while(isdigit((unsigned char)*(com+1))) com++;
  1006.             } else opt->robots=1;
  1007. #if DEBUG_ROBOTS
  1008.             printf("robots.txt mode set to %d\n",opt->robots);
  1009. #endif
  1010.             break;
  1011.           case 'o': sscanf(com+1,"%d",&opt->errpage); while(isdigit((unsigned char)*(com+1))) com++; break;
  1012.           case 'u': sscanf(com+1,"%d",&opt->check_type); while(isdigit((unsigned char)*(com+1))) com++; break;
  1013.             //
  1014.           case 'C': 
  1015.             if (isdigit((unsigned char)*(com+1))) {
  1016.               sscanf(com+1,"%d",&opt->cache);
  1017.               while(isdigit((unsigned char)*(com+1))) com++;
  1018.             } else opt->cache=1;
  1019.             break;
  1020.           case 'k': opt->all_in_cache=1; break;
  1021.             //
  1022.           case 'z': opt->debug=1; break;  // petit debug
  1023.           case 'Z': opt->debug=2; break;  // GROS debug
  1024.             //
  1025.           case '&': case '%': {    // deuxiΦme jeu d'options
  1026.             com++;
  1027.             switch(*com) {
  1028.             case 'M': opt->mimehtml = 1; if (*(com+1)=='0') { opt->mimehtml=0; com++; } break;
  1029.             case 'k': opt->nokeepalive = 0; if (*(com+1)=='0') { opt->nokeepalive = 1; com++; } break;
  1030.             case 'x': opt->passprivacy=1; if (*(com+1)=='0') { opt->passprivacy=0; com++; } break;   // No passwords in html files
  1031.             case 'q': opt->includequery=1; if (*(com+1)=='0') { opt->includequery=0; com++; } break;   // No passwords in html files
  1032.             case 'I': opt->kindex=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&opt->kindex); while(isdigit((unsigned char)*(com+1))) com++; }
  1033.               break;    // Keyword Index
  1034.             case 'c': sscanf(com+1,"%f",&opt->maxconn); while(isdigit((unsigned char)*(com+1)) || *(com+1) == '.') com++; break;
  1035.             case 'e': sscanf(com+1,"%d",&opt->extdepth); while(isdigit((unsigned char)*(com+1))) com++; break;
  1036.             case 'B': opt->tolerant=1; if (*(com+1)=='0') { opt->tolerant=0; com++; } break;   // HTTP/1.0 notamment
  1037.             case 'h': opt->http10=1; if (*(com+1)=='0') { opt->http10=0; com++; } break;   // HTTP/1.0
  1038.             case 'z': opt->nocompression=1; if (*(com+1)=='0') { opt->nocompression=0; com++; } break;   // pas de compression
  1039.             case 'f': opt->ftp_proxy=1; if (*(com+1)=='0') { opt->ftp_proxy=0; com++; } break;   // proxy http pour ftp
  1040.             case 'P': opt->parseall=1; if (*(com+1)=='0') { opt->parseall=0; com++; } break;   // tout parser
  1041.             case 'n': opt->norecatch=1; if (*(com+1)=='0') { opt->norecatch=0; com++; } break;   // ne pas reprendre fichiers effacΘs localement
  1042.             case 's': opt->sizehack=1; if (*(com+1)=='0') { opt->sizehack=0; com++; } break;   // hack sur content-length
  1043.             case 'u': opt->urlhack=1; if (*(com+1)=='0') { opt->urlhack=0; com++; } break;   // url hack
  1044.             case 'v': opt->verbosedisplay=2; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&opt->verbosedisplay); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1045.             case 'i': opt->dir_topindex = 1; if (*(com+1)=='0') { opt->dir_topindex=0; com++; } break;
  1046.             case 'N': opt->savename_delayed = 2; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&opt->savename_delayed); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1047.             case 'D': opt->delayed_cached=1; if (*(com+1)=='0') { opt->delayed_cached=0; com++; } break;   // url hack
  1048.             case '!': opt->bypass_limits = 1; if (*(com+1)=='0') { opt->bypass_limits=0; com++; } break;
  1049. #if HTS_USEMMS
  1050.                         case 'm': sscanf(com+1,"%d",&opt->mms_maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1051. #endif
  1052.             case 'w':   // disable specific plugin
  1053.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1054.                 HTS_PANIC_PRINTF("Option %w needs to be followed by a blank space, and a module name");
  1055.                 printf("Example: -%%w htsswf\n");
  1056.                 htsmain_free();
  1057.                 return -1;
  1058.               } else{
  1059.                 na++;
  1060.                 StringCat(opt->mod_blacklist, argv[na]);
  1061.                 StringCat(opt->mod_blacklist, "\n");
  1062.               }
  1063.               break;
  1064.  
  1065.             // preserve: no footer, original links
  1066.             case 'p':
  1067.               StringClear(opt->footer);
  1068.               opt->urlmode=4;
  1069.               break;
  1070.             case 'L':    // URL list
  1071.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1072.                 HTS_PANIC_PRINTF("Option %L needs to be followed by a blank space, and a text filename");
  1073.                 printf("Example: -%%L \"mylist.txt\"\n");
  1074.                 htsmain_free();
  1075.                 return -1;
  1076.               } else{
  1077.                 na++;
  1078.                 if (strlen(argv[na])>=254) {
  1079.                   HTS_PANIC_PRINTF("File list string too long");
  1080.                   htsmain_free();
  1081.                   return -1;
  1082.                 }
  1083.                 StringCopy(opt->filelist,argv[na]);
  1084.               }
  1085.               break;
  1086.             case 'b':  // bind
  1087.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1088.                 HTS_PANIC_PRINTF("Option %b needs to be followed by a blank space, and a local hostname");
  1089.                 printf("Example: -%%b \"ip4.localhost\"\n");
  1090.                 htsmain_free();
  1091.                 return -1;
  1092.               } else{
  1093.                 na++;
  1094.                 if (strlen(argv[na])>=254) {
  1095.                   HTS_PANIC_PRINTF("Hostname string too long");
  1096.                   htsmain_free();
  1097.                   return -1;
  1098.                 }
  1099.                 StringCopy(opt->proxy.bindhost, argv[na]);
  1100.               }
  1101.               break;
  1102.             case 'S':    // Scan Rules list
  1103.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1104.                 HTS_PANIC_PRINTF("Option %S needs to be followed by a blank space, and a text filename");
  1105.                 printf("Example: -%%S \"myfilterlist.txt\"\n");
  1106.                 htsmain_free();
  1107.                 return -1;
  1108.               } else{
  1109.                 off_t fz;
  1110.                 na++;
  1111.                 fz = fsize(argv[na]);
  1112.                 if (fz < 0) {
  1113.                   HTS_PANIC_PRINTF("File url list could not be opened");
  1114.                   htsmain_free();
  1115.                   return -1;
  1116.                 } else {
  1117.                   FILE* fp = fopen(argv[na], "rb");
  1118.                   if (fp != NULL) {
  1119.                     int cl = (int) strlen(url);
  1120.                     ensureUrlCapacity(url, url_sz, cl + fz + 8192);
  1121.                     if (fread(url + cl, 1, fz, fp) != fz) {
  1122.                       HTS_PANIC_PRINTF("File url list could not be read");
  1123.                       htsmain_free();
  1124.                       return -1;
  1125.                     }
  1126.                     fclose(fp);
  1127.                     *(url + cl + fz) = '\0';
  1128.                   }
  1129.                 }
  1130.               }
  1131.               break;
  1132.             case 'A':    // assume
  1133.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1134.                 HTS_PANIC_PRINTF("Option %A needs to be followed by a blank space, and a filesystemtype=mimetype/mimesubtype parameters");
  1135.                 printf("Example: -%%A php3=text/html,asp=text/html\n");
  1136.                 htsmain_free();
  1137.                 return -1;
  1138.               } else{
  1139.                 na++;
  1140.                 // --assume standard
  1141.                 if (strcmp(argv[na], "standard") == 0) {
  1142.                   StringCopy(opt->mimedefs,"\n");
  1143.                   StringCat(opt->mimedefs,HTS_ASSUME_STANDARD);
  1144.                   StringCat(opt->mimedefs,"\n");
  1145.                 } else {
  1146.                   char* a;
  1147.                   //char* b = StringBuff(opt->mimedefs) + StringLength(opt->mimedefs);
  1148.                   for(a = argv[na] ; *a != '\0' ; a++) {
  1149.                     if (*a == ';') {    /* next one */
  1150.                       StringAddchar(opt->mimedefs, '\n');
  1151.                       //*b++ = '\n';
  1152.                     } else if (*a == ',' || *a == '\n' || *a == '\r' || *a == '\t') {
  1153.                       StringAddchar(opt->mimedefs, ' ');
  1154.                       //*b++ = ' ';
  1155.                     } else {
  1156.                       StringAddchar(opt->mimedefs, *a);
  1157.                       //*b++ = *a;
  1158.                     }
  1159.                   }
  1160.                   StringAddchar(opt->mimedefs, '\n');
  1161.                   //*b++ = '\n';    /* next def */
  1162.                   //*b++ = '\0';
  1163.                 }
  1164.               }
  1165.               break;
  1166.               //
  1167.             case 'l': 
  1168.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1169.                 HTS_PANIC_PRINTF("Option %l needs to be followed by a blank space, and an ISO language code");
  1170.                 printf("Example: -%%l \"en\"\n");
  1171.                 htsmain_free();
  1172.                 return -1;
  1173.               } else{
  1174.                 na++;
  1175.                 if (strlen(argv[na])>=62) {
  1176.                   HTS_PANIC_PRINTF("Lang list string too long");
  1177.                   htsmain_free();
  1178.                   return -1;
  1179.                 }
  1180.                 StringCopy(opt->lang_iso,argv[na]);
  1181.               }
  1182.               break;
  1183.               //
  1184.             case 'F':     // footer id
  1185.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1186.                 HTS_PANIC_PRINTF("Option %F needs to be followed by a blank space, and a footer string");
  1187.                 printf("Example: -%%F \"<!-- Mirrored from %%s by HTTrack Website Copier/"HTTRACK_AFF_VERSION" "HTTRACK_AFF_AUTHORS", %%s -->\"\n");
  1188.                 htsmain_free();
  1189.                 return -1;
  1190.               } else{
  1191.                 na++;
  1192.                 if (strlen(argv[na])>=254) {
  1193.                   HTS_PANIC_PRINTF("Footer string too long");
  1194.                   htsmain_free();
  1195.                   return -1;
  1196.                 }
  1197.                 StringCopy(opt->footer,argv[na]);
  1198.               }
  1199.               break;
  1200.             case 'H':                 // debug headers
  1201.               _DEBUG_HEAD=1;
  1202.               break;
  1203.             case 'O':
  1204. #ifdef _WIN32
  1205.               printf("Warning option -%%O has no effect in this system (chroot)\n");
  1206. #else
  1207.               switch_chroot=1;
  1208. #endif
  1209.               break;
  1210.             case 'U':                 // setuid
  1211.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1212.                 HTS_PANIC_PRINTF("Option %U needs to be followed by a blank space, and a username");
  1213.                 printf("Example: -%%U smith\n");
  1214.                 htsmain_free();
  1215.                 return -1;
  1216.               } else {
  1217.                 na++;
  1218. #ifdef _WIN32
  1219.                 printf("Warning option -%%U has no effect on this system (setuid)\n");
  1220. #else
  1221. #ifndef HTS_DO_NOT_USE_UID
  1222.                 /* Change the user id and gid */
  1223.                 {
  1224.                   struct passwd* userdef=getpwnam((const char*)argv[na]);
  1225.                   if (userdef) {    /* we'll have to switch the user id */
  1226.                     switch_gid=userdef->pw_gid;
  1227.                     switch_uid=userdef->pw_uid;
  1228.                   }
  1229.                 }
  1230. #else
  1231.                 printf("Warning option -%%U has no effect with this compiled version (setuid)\n");
  1232. #endif
  1233. #endif
  1234.               }
  1235.               break;
  1236.               
  1237.             case 'W':       // Wrapper callback
  1238.               // --wrapper check-link=obj.so:check_link
  1239.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1240.                 HTS_PANIC_PRINTF("Option %W needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1241.                 printf("Example: -%%W check-link=checklink.so:check\n");
  1242.                 htsmain_free();
  1243.                 return -1;
  1244.               } else {
  1245.                 char callbackname[128];
  1246.                 char* a = argv[na + 1];
  1247.                 char* pos; /*  = strchr(a, '='); */
  1248.                 for(pos = a ; *pos != '\0' && *pos != '=' && *pos != ',' && *pos != ':' ; pos++);
  1249.                 /* httrack --wrapper callback[,foo] */
  1250.                 if (*pos == 0 || *pos == ',' || *pos == ':') {
  1251.                   int ret = plug_wrapper(opt, argv[na + 1], argv[na + 1]);
  1252.                   if (ret == 0) {
  1253.                     char BIGSTK tmp[1024 * 2];
  1254.                     sprintf(tmp, "option %%W : unable to plug the module %s (returncode != 1)", a);
  1255.                     HTS_PANIC_PRINTF(tmp);
  1256.                     htsmain_free();
  1257.                     return -1;
  1258.                   } else if (ret == -1) {
  1259.                     char BIGSTK tmp[1024 * 2];
  1260.                     int last_errno = errno;
  1261.                     sprintf(tmp, "option %%W : unable to load the module %s: %s (check the library path ?)", a, strerror(last_errno));
  1262.                     HTS_PANIC_PRINTF(tmp);
  1263.                     htsmain_free();
  1264.                     return -1;
  1265.                   }
  1266.                 }
  1267.                 /* Old style */
  1268.                 /* httrack --wrapper save-name=callback:process,string */
  1269.                 else if (*pos == '=' && (pos - a) > 0 && (pos - a + 2) < sizeof(callbackname)) {
  1270.                   fprintf(stderr, "Syntax error in option %%W : the old (<3.41) API is no more supported!\n");
  1271.                   HTS_PANIC_PRINTF("Syntax error in option %W : this function needs to be followed by a blank space, and a module name");
  1272.                   printf("Example: -%%W check-link=checklink.so:check\n");
  1273.                   htsmain_free();
  1274.                   return -1;
  1275.                 } else {
  1276.                   HTS_PANIC_PRINTF("Syntax error in option %W : this function needs to be followed by a blank space, and a module name");
  1277.                   printf("Example: -%%W check-link=checklink.so:check\n");
  1278.                   htsmain_free();
  1279.                   return -1;
  1280.                 }
  1281.               }
  1282.               break;
  1283.               
  1284.             case 'R':    // Referer
  1285.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1286.                 HTS_PANIC_PRINTF("Option %R needs to be followed by a blank space, and a referer URL");
  1287.                 printf("Example: -%%R \"http://www.example.com/\"\n");
  1288.                 htsmain_free();
  1289.                 return -1;
  1290.               } else{
  1291.                 na++;
  1292.                 if (strlen(argv[na])>=254) {
  1293.                   HTS_PANIC_PRINTF("Referer URL too long");
  1294.                   htsmain_free();
  1295.                   return -1;
  1296.                 }
  1297.                 StringCopy(opt->referer, argv[na]);
  1298.               }
  1299.               break;
  1300.             case 'E':    // From Email address
  1301.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1302.                 HTS_PANIC_PRINTF("Option %E needs to be followed by a blank space, and an email");
  1303.                 printf("Example: -%%E \"postmaster@example.com\"\n");
  1304.                 htsmain_free();
  1305.                 return -1;
  1306.               } else{
  1307.                 na++;
  1308.                 if (strlen(argv[na])>=254) {
  1309.                   HTS_PANIC_PRINTF("From email too long");
  1310.                   htsmain_free();
  1311.                   return -1;
  1312.                 }
  1313.                 StringCopy(opt->from, argv[na]);
  1314.               }
  1315.               break;
  1316.  
  1317.             default: {
  1318.               char s[HTS_CDLMAXSIZE];
  1319.               sprintf(s,"invalid option %%%c\n",*com);
  1320.               HTS_PANIC_PRINTF(s);
  1321.               htsmain_free();
  1322.               return -1;
  1323.                      }
  1324.               break;
  1325.               
  1326.             }
  1327.                     }
  1328.             break;
  1329.             //
  1330.           case '@': {    // troisiΦme jeu d'options
  1331.             com++;
  1332.             switch(*com) {
  1333.             case 'i': 
  1334. #if HTS_INET6==0
  1335.               printf("Warning, option @i has no effect (v6 routines not compiled)\n");
  1336. #else 
  1337.               {
  1338.                 int res=0;
  1339.                 if (isdigit((unsigned char)*(com+1))) {
  1340.                   sscanf(com+1,"%d",&res); while(isdigit((unsigned char)*(com+1))) com++; 
  1341.                 }
  1342.                 switch(res) {
  1343.                 case 1:
  1344.                 case 4:
  1345.                   IPV6_resolver=1;
  1346.                   break;
  1347.                 case 2:
  1348.                 case 6:
  1349.                   IPV6_resolver=2;
  1350.                   break;
  1351.                 case 0:
  1352.                   IPV6_resolver=0;
  1353.                   break;
  1354.                 default:
  1355.                   printf("Unknown flag @i%d\n", res);
  1356.                   htsmain_free();
  1357.                   return -1;
  1358.                   break;
  1359.                 }
  1360.               }
  1361. #endif
  1362.               break;
  1363.               
  1364.                 default: {
  1365.                   char s[HTS_CDLMAXSIZE];
  1366.                   sprintf(s,"invalid option %%%c\n",*com);
  1367.                   HTS_PANIC_PRINTF(s);
  1368.                   htsmain_free();
  1369.                   return -1;
  1370.                          }
  1371.                   break;
  1372.                   
  1373.                   //case 's': opt->sslengine=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&opt->sslengine); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1374.             }
  1375.                     }
  1376.             break;
  1377.             
  1378.             //
  1379.           case '#':  { // non documentΘ
  1380.             com++;
  1381.             switch(*com) {
  1382.             case 'C':   // list cache files : httrack -#C '*spid*.gif' will attempt to find the matching file
  1383.               {
  1384.                 int hasFilter = 0;
  1385.                 int found = 0;
  1386.                 char* filter=NULL;
  1387.                 cache_back cache;
  1388.                 inthash cache_hashtable=inthash_new(HTS_HASH_SIZE);
  1389.                 int backupXFR = htsMemoryFastXfr;
  1390.                 int sendb = 0;
  1391.                 if (isdigit((unsigned char)*(com+1))) {
  1392.                   sscanf(com+1,"%d",&sendb);
  1393.                   while(isdigit((unsigned char)*(com+1))) com++;
  1394.                 } else sendb=0;
  1395.                 if (!((na+1>=argc) || (argv[na+1][0]=='-'))) {
  1396.                   na++;
  1397.                   hasFilter = 1;
  1398.                   filter=argv[na];
  1399.                 }
  1400.                 htsMemoryFastXfr = 1;               /* fast load */
  1401.  
  1402.                 memset(&cache, 0, sizeof(cache_back));
  1403.                 cache.type=1;       // cache?
  1404.                 cache.log=stdout;     // log?
  1405.                 cache.errlog=stderr;  // err log?
  1406.                 cache.ptr_ant=cache.ptr_last=0;   // pointeur pour anticiper
  1407.                 cache.hashtable=(void*)cache_hashtable;      /* copy backcache hash */
  1408.                 cache.ro = 1;          /* read only */
  1409.                 if (cache.hashtable) {
  1410.                   char BIGSTK adr[HTS_URLMAXSIZE*2];
  1411.                   char BIGSTK fil[HTS_URLMAXSIZE*2];
  1412.                   char BIGSTK url[HTS_URLMAXSIZE*2];
  1413.                   char linepos[256];
  1414.                   int  pos;
  1415.                   char* cacheNdx = readfile(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));
  1416.                   cache_init(&cache,opt);            /* load cache */
  1417.                   if (cacheNdx != NULL) {
  1418.                     char firstline[256];
  1419.                     char* a = cacheNdx;
  1420.                     a+=cache_brstr(a, firstline);
  1421.                     a+=cache_brstr(a, firstline);
  1422.                     while ( a != NULL ) {
  1423.                       a=strchr(a+1,'\n');     /* start of line */
  1424.                       if (a) {
  1425.                         htsblk r;
  1426.                         /* */
  1427.                         a++;
  1428.                         /* read "host/file" */
  1429.                         a+=binput(a,adr,HTS_URLMAXSIZE);
  1430.                         a+=binput(a,fil,HTS_URLMAXSIZE);
  1431.                         url[0]='\0';
  1432.                         if (!link_has_authority(adr))
  1433.                           strcatbuff(url, "http://");
  1434.                         strcatbuff(url, adr);
  1435.                         strcatbuff(url, fil);
  1436.                         /* read position */
  1437.                         a+=binput(a,linepos,200);
  1438.                         sscanf(linepos,"%d",&pos);
  1439.                         if (!hasFilter
  1440.                           ||
  1441.                           (strjoker(url, filter, NULL, NULL) != NULL)
  1442.                           ) {
  1443.                           r = cache_read_ro(opt, &cache, adr, fil, "", NULL);    // lire entrΘe cache + data
  1444.                           if (r.statuscode != -1) {    // No errors
  1445.                             found++;
  1446.                             if (!hasFilter) {
  1447.                               fprintf(stdout, "%s%s%s\r\n", 
  1448.                                 (link_has_authority(adr)) ? "" : "http://", 
  1449.                                 adr, fil);
  1450.                             } else {
  1451.                               char msg[256], cdate[256];
  1452.                               char BIGSTK sav[HTS_URLMAXSIZE*2];
  1453.                               infostatuscode(msg, r.statuscode);
  1454.                               time_gmt_rfc822(cdate);
  1455.  
  1456.                               fprintf(stdout, "HTTP/1.1 %d %s\r\n",
  1457.                                 r.statuscode,
  1458.                                 r.msg[0] ? r.msg : msg
  1459.                                 );
  1460.                               fprintf(stdout, "X-Host: %s\r\n", adr);
  1461.                               fprintf(stdout, "X-File: %s\r\n", fil);
  1462.                               fprintf(stdout, "X-URL: %s%s%s\r\n", 
  1463.                                 (link_has_authority(adr)) ? "" : "http://", 
  1464.                                 adr, fil);
  1465.                               if (url_savename(adr, fil, sav, /*former_adr*/NULL, /*former_fil*/NULL, /*referer_adr*/NULL, /*referer_fil*/NULL,
  1466.                                 /*opt*/opt, /*liens*/NULL, /*lien_tot*/0, /*sback*/NULL, /*cache*/&cache, /*hash*/NULL, /*ptr*/0, /*numero_passe*/0, /*mime_type*/NULL)!=-1) {
  1467.                                 if (fexist(sav)) {
  1468.                                   fprintf(stdout, "Content-location: %s\r\n", sav);
  1469.                                 }
  1470.                               }
  1471.                               fprintf(stdout, "Date: %s\r\n", cdate);
  1472.                               fprintf(stdout, "Server: HTTrack Website Copier/"HTTRACK_VERSION"\r\n");
  1473.                               if (r.lastmodified[0]) {
  1474.                                 fprintf(stdout, "Last-Modified: %s\r\n", r.lastmodified);
  1475.                               }
  1476.                               if (r.etag[0]) {
  1477.                                 fprintf(stdout, "Etag: %s\r\n", r.etag);
  1478.                               }
  1479.                               if (r.totalsize >= 0) {
  1480.                                 fprintf(stdout, "Content-Length: "LLintP"\r\n", r.totalsize);
  1481.                               }
  1482.                               fprintf(stdout, "X-Content-Length: "LLintP"\r\n", (r.size >= 0) ? r.size : (-r.size) );
  1483.                               if (r.contenttype >= 0) {
  1484.                                 fprintf(stdout, "Content-Type: %s\r\n", r.contenttype);
  1485.                               }
  1486.                               if (r.cdispo[0]) {
  1487.                                 fprintf(stdout, "Content-Disposition: %s\r\n", r.cdispo);
  1488.                               }
  1489.                               if (r.contentencoding[0]) {
  1490.                                 fprintf(stdout, "Content-Encoding: %s\r\n", r.contentencoding);
  1491.                               }
  1492.                               if (r.is_chunk) {
  1493.                                 fprintf(stdout, "Transfer-Encoding: chunked\r\n");
  1494.                               }
  1495. #if HTS_USEOPENSSL
  1496.                               if (r.ssl) {
  1497.                                 fprintf(stdout, "X-SSL: yes\r\n");
  1498.                               }
  1499. #endif
  1500.                               if (r.is_write) {
  1501.                                 fprintf(stdout, "X-Direct-To-Disk: yes\r\n");
  1502.                               }
  1503.                               if (r.compressed) {
  1504.                                 fprintf(stdout, "X-Compressed: yes\r\n");
  1505.                               }
  1506.                               if (r.notmodified) {
  1507.                                 fprintf(stdout, "X-Not-Modified: yes\r\n");
  1508.                               }
  1509.                               if (r.is_chunk) {
  1510.                                 fprintf(stdout, "X-Chunked: yes\r\n");
  1511.                               }
  1512.                               fprintf(stdout, "\r\n");
  1513.                               /* Send the body */
  1514.                               if (sendb && r.adr) {
  1515.                                 fprintf(stdout, "%s\r\n", r.adr);
  1516.                               }
  1517.                             }
  1518.                           }
  1519.                         }
  1520.                       }
  1521.                     }
  1522.                     freet(cacheNdx);
  1523.                   }
  1524.                 }
  1525.                 if (!found) {
  1526.                   fprintf(stderr, "No cache entry found%s%s%s\r\n",
  1527.                     (hasFilter)?" for '":"",
  1528.                     (hasFilter)?filter:"",
  1529.                     (hasFilter)?"'":""
  1530.                     );
  1531.                 }
  1532.                 htsMemoryFastXfr = backupXFR;
  1533.                 return 0;
  1534.               }
  1535.               break;
  1536.             case 'E':     // extract cache
  1537.               if (!hts_extract_meta(StringBuff(opt->path_log))) {
  1538.                 fprintf(stderr, "* error extracting meta-data\n");
  1539.                 return 1;
  1540.               }
  1541.               fprintf(stderr, "* successfully extracted meta-data\n");
  1542.               return 0;
  1543.               break;
  1544.             case 'X': 
  1545. #ifndef STRDEBUG
  1546.               fprintf(stderr, "warning: no string debugging support built, option has no effect\n");
  1547. #endif
  1548.               htsMemoryFastXfr=1; 
  1549.               if (*(com+1)=='0') { htsMemoryFastXfr=0; com++; } 
  1550.               break;
  1551.             case 'R':
  1552.               {
  1553.                 char* name;
  1554.                 uLong repaired = 0;
  1555.                 uLong repairedBytes = 0;
  1556.                 if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"))) {
  1557.                   name = fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip");
  1558.                 } else if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"))) {
  1559.                   name = fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip");
  1560.                 } else {
  1561.                   fprintf(stderr, "* error: no cache found in %s\n", fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"));
  1562.                   return 1;
  1563.                 }
  1564.                 fprintf(stderr, "Cache: trying to repair %s\n", name);
  1565.                 if (unzRepair(name, 
  1566.                   fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.zip"),
  1567.                   fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.tmp"),
  1568.                   &repaired, &repairedBytes
  1569.                   ) == Z_OK) {
  1570.                   unlink(name);
  1571.                   rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/repair.zip"), name);
  1572.                   fprintf(stderr,"Cache: %d bytes successfully recovered in %d entries\n", (int) repairedBytes, (int) repaired);
  1573.                 } else {
  1574.                   fprintf(stderr, "Cache: could not repair the cache\n");
  1575.                 }
  1576.               }
  1577.               return 0;
  1578.               break;
  1579.             case '~': /* internal lib test */
  1580.               {
  1581.                 char thisIsATestYouShouldSeeAnError[12];
  1582.                 strcpybuff(thisIsATestYouShouldSeeAnError, "0123456789012345678901234567890123456789");
  1583.                 return 0;
  1584.               }
  1585.               break;
  1586.             case 'f': opt->flush=1; break;
  1587.             case 'h':
  1588.               printf("HTTrack version "HTTRACK_VERSION"%s\n", hts_get_version_info(opt));
  1589.               return 0;
  1590.               break;
  1591.             case 'p': /* opt->aff_progress=1; deprecated */ break;
  1592.             case 'S': opt->shell=1; break;  // stdin sur un shell
  1593.             case 'K': opt->keyboard=1; break;  // vΘrifier stdin
  1594.               //
  1595.             case 'L': sscanf(com+1,"%d",&opt->maxlink); while(isdigit((unsigned char)*(com+1))) com++; break;
  1596.             case 'F': sscanf(com+1,"%d",&opt->maxfilter); while(isdigit((unsigned char)*(com+1))) com++; break;
  1597.             case 'Z': opt->makestat=1; break;
  1598.             case 'T': opt->maketrack=1; break;
  1599.             case 'u': sscanf(com+1,"%d",&opt->waittime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1600.  
  1601.             /*case 'R':    // ohh ftp, catch->ftpget
  1602.               HTS_PANIC_PRINTF("Unexpected internal error with -#R command");
  1603.               htsmain_free();
  1604.               return -1;        
  1605.               break;
  1606.               */
  1607.             case 'P': {     // catchurl
  1608.               help_catchurl(StringBuff(opt->path_log));
  1609.               htsmain_free();
  1610.               return 0;
  1611.                       }
  1612.               break;
  1613.           
  1614.             case '0':   /* test #0 : filters */
  1615.               if (na+2>=argc) {
  1616.                 HTS_PANIC_PRINTF("Option #0 needs to be followed by a filter string and a string");
  1617.                 printf("Example: '-#0' '*.gif' 'foo.gif'\n");
  1618.                 htsmain_free();
  1619.                 return -1;
  1620.               } else {
  1621.                 if (strjoker(argv[na+2],argv[na+1],NULL,NULL))
  1622.                   printf("%s does match %s\n",argv[na+2],argv[na+1]);
  1623.                 else
  1624.                   printf("%s does NOT match %s\n",argv[na+2],argv[na+1]);
  1625.                 htsmain_free();
  1626.                 return 0;
  1627.               }
  1628.               break;
  1629.             case '1':   /* test #1 : fil_simplifie */
  1630.               if (na+1>=argc) {
  1631.                 HTS_PANIC_PRINTF("Option #1 needs to be followed by an URL");
  1632.                 printf("Example: '-#1' ./foo/bar/../foobar\n");
  1633.                 htsmain_free();
  1634.                 return -1;
  1635.               } else {
  1636.                 fil_simplifie(argv[na+1]);
  1637.                 printf("simplified=%s\n", argv[na+1]);
  1638.                 htsmain_free();
  1639.                 return 0;
  1640.               }
  1641.               break;
  1642.             case '2':   // mimedefs
  1643.               if (na+1>=argc) {
  1644.                 HTS_PANIC_PRINTF("Option #1 needs to be followed by an URL");
  1645.                 printf("Example: '-#2' /foo/bar.php\n");
  1646.                 htsmain_free();
  1647.                 return -1;
  1648.               } else {
  1649.                 char mime[256];
  1650.                 // initialiser mimedefs
  1651.                 //get_userhttptype(opt,1,opt->mimedefs,NULL);
  1652.                 // check
  1653.                 mime[0] = '\0';
  1654.                 get_httptype(opt, mime, argv[na+1], 0);
  1655.                 if (mime[0] != '\0') {
  1656.                   char ext[256];
  1657.                   printf("%s is '%s'\n", argv[na+1], mime);
  1658.                   ext[0] = '\0';
  1659.                   give_mimext(ext, mime);
  1660.                   if (ext[0]) {
  1661.                     printf("and its local type is '.%s'\n", ext);
  1662.                   }
  1663.                 } else {
  1664.                   printf("%s is of an unknown MIME type\n", argv[na+1]);
  1665.                 }
  1666.                 htsmain_free();
  1667.                 return 0;
  1668.               }
  1669.               break;
  1670.             case '!':
  1671.               if (na+1>=argc) {
  1672.                 HTS_PANIC_PRINTF("Option #! needs to be followed by a commandline");
  1673.                 printf("Example: '-#!' 'echo hello'\n");
  1674.                 htsmain_free();
  1675.                 return -1;
  1676.               } else {
  1677.                 system(argv[na+1]);
  1678.               }
  1679.               break;
  1680.             case 'd':
  1681.               opt->parsedebug = 1;
  1682.               break;
  1683.  
  1684.             /* autotest */
  1685.             case 't':     /* not yet implemented */
  1686.               fprintf(stderr, "** AUTOCHECK OK\n");
  1687.               exit(0);
  1688.               break;
  1689.  
  1690.             default: printf("Internal option %c not recognized\n",*com); break;
  1691.             }
  1692.                      }
  1693.             break; 
  1694.           case 'O':    // output path
  1695.                         while(isdigit(com[1])) {
  1696.                             com++;
  1697.                         }
  1698.             na++;     // sauter, dΘja traitΘ
  1699.             break;
  1700.           case 'P':    // proxy
  1701.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1702.               HTS_PANIC_PRINTF("Option P needs to be followed by a blank space, and a proxy proxy:port or user:id@proxy:port");
  1703.               printf("Example: -P proxy.myhost.com:8080\n");
  1704.               htsmain_free();
  1705.               return -1;
  1706.             } else {
  1707.               char* a;
  1708.               na++;
  1709.               opt->proxy.active=1;
  1710.               // Rechercher MAIS en partant de la fin α cause de user:pass@proxy:port
  1711.               a = argv[na] + strlen(argv[na]) -1;
  1712.               // a=strstr(argv[na],":");  // port
  1713.               while( (a > argv[na]) && (*a != ':') && (*a != '@') ) a--;
  1714.               if (*a == ':') {  // un port est prΘsent, <proxy>:port
  1715.                 sscanf(a+1,"%d",&opt->proxy.port);
  1716.                 StringCopyN(opt->proxy.name,argv[na],(int) (a - argv[na]));
  1717.               } else {  // <proxy>
  1718.                 opt->proxy.port=8080;
  1719.                 StringCopy(opt->proxy.name,argv[na]);
  1720.               }
  1721.             }
  1722.             break;
  1723.           case 'F':    // user-agent field
  1724.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1725.               HTS_PANIC_PRINTF("Option F needs to be followed by a blank space, and a user-agent name");
  1726.               printf("Example: -F \"my_user_agent/1.0\"\n");
  1727.               htsmain_free();
  1728.               return -1;
  1729.             } else{
  1730.               na++;
  1731.               if (strlen(argv[na])>=126) {
  1732.                 HTS_PANIC_PRINTF("User-agent length too long");
  1733.                 htsmain_free();
  1734.                 return -1;
  1735.               }
  1736.               StringCopy(opt->user_agent,argv[na]);
  1737.               if (StringNotEmpty(opt->user_agent))
  1738.                 opt->user_agent_send=1;
  1739.               else
  1740.                 opt->user_agent_send=0;    // -F "" dΘsactive l'option
  1741.             }
  1742.             break;
  1743.             //
  1744.           case 'V':    // execute command
  1745.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1746.               HTS_PANIC_PRINTF("Option V needs to be followed by a system-command string");
  1747.               printf("Example: -V \"tar uvf some.tar \\$0\"\n");
  1748.               htsmain_free();
  1749.               return -1;
  1750.             } else{
  1751.               na++;
  1752.               if (strlen(argv[na])>=2048) {
  1753.                 HTS_PANIC_PRINTF("System-command length too long");
  1754.                 htsmain_free();
  1755.                 return -1;
  1756.               }
  1757.               StringCopy(opt->sys_com,argv[na]);
  1758.               if (StringNotEmpty(opt->sys_com))
  1759.                 opt->sys_com_exec=1;
  1760.               else
  1761.                 opt->sys_com_exec=0;    // -V "" dΘsactive l'option
  1762.             }
  1763.             break;
  1764.             //
  1765.           default: {
  1766.             char s[HTS_CDLMAXSIZE];
  1767.             sprintf(s,"invalid option %c\n",*com);
  1768.             HTS_PANIC_PRINTF(s);
  1769.             htsmain_free();
  1770.             return -1;
  1771.                    }
  1772.             break;
  1773.           }  // switch
  1774.           com++;    
  1775.         }  // while
  1776.         
  1777.       }  else {  // URL/filters
  1778.                 char catbuff[CATBUFF_SIZE];
  1779.         char BIGSTK tempo[1024];       
  1780.         if (strnotempty(url)) strcatbuff(url," ");  // espace de sΘparation
  1781.         strcpybuff(tempo,unescape_http_unharm(catbuff,argv[na],1));
  1782.         escape_spc_url(tempo);
  1783.         strcatbuff(url,tempo);
  1784.       }  // if argv=- etc. 
  1785.       
  1786.     }  // for
  1787.   }
  1788.   
  1789. #if BDEBUG==3  
  1790.   printf("URLs/filters=%s\n",url);
  1791. #endif
  1792.  
  1793. #if DEBUG_STEPS
  1794.   printf("Analyzing parameters done\n");
  1795. #endif
  1796.  
  1797.  
  1798. #ifdef _WIN32
  1799. #else
  1800. #ifndef HTS_DO_NOT_USE_UID
  1801.   /* Chroot - xxc */
  1802.   if (switch_chroot) {
  1803.     uid_t userid=getuid();
  1804.     //struct passwd* userdef=getpwuid(userid);
  1805.     //if (userdef) {
  1806.     if (!userid) {
  1807.       //if (strcmp(userdef->pw_name,"root")==0) {
  1808.       char BIGSTK rpath[1024];
  1809.       //printf("html=%s log=%s\n",StringBuff(opt->path_html),StringBuff(opt->path_log));    // xxc
  1810.       if ((StringBuff(opt->path_html)[0]) && (StringBuff(opt->path_log)[0])) {
  1811.         const char *a=StringBuff(opt->path_html),*b=StringBuff(opt->path_log),*c=NULL,*d=NULL;
  1812.         c=a; d=b;
  1813.         while ((*a) && (*a == *b)) {
  1814.           if (*a=='/') { c=a; d=b; }
  1815.           a++;
  1816.           b++;
  1817.         }
  1818.  
  1819.         rpath[0]='\0';
  1820.         if (c != StringBuff(opt->path_html)) {
  1821.           if (StringBuff(opt->path_html)[0]!='/')
  1822.             strcatbuff(rpath,"./");
  1823.           strncatbuff(rpath,StringBuff(opt->path_html),(int) (c - StringBuff(opt->path_html)));
  1824.         }
  1825.         StringCopyOverlapped(opt->path_html, c);
  1826.         StringCopyOverlapped(opt->path_log, d);
  1827.       } else {
  1828.         strcpybuff(rpath,"./");
  1829.         StringCopy(opt->path_html,"/");
  1830.         StringCopy(opt->path_log,"/");
  1831.       }
  1832.       if (rpath[0]) {
  1833.         printf("[changing root path to %s (path_data=%s,path_log=%s)]\n",rpath,StringBuff(opt->path_html),StringBuff(opt->path_log));
  1834.         if (chroot(rpath)) {
  1835.           printf("ERROR! Can not chroot to %s!\n",rpath);
  1836.           return -1;
  1837.         }
  1838.         if (chdir("/")) {     /* new root */
  1839.           printf("ERROR! Can not chdir to %s!\n",rpath);
  1840.           return -1;
  1841.         }
  1842.       } else
  1843.         printf("WARNING: chroot not possible with these paths\n");
  1844.     }
  1845.     //}
  1846.   }
  1847.  
  1848.   /* Setuid */
  1849.   if (switch_uid>=0) {
  1850.     printf("[setting user/group to %d/%d]\n",switch_uid,switch_gid);
  1851.     if (setgid(switch_gid))
  1852.       printf("WARNING! Can not setgid to %d!\n",switch_gid);
  1853.     if (setuid(switch_uid))
  1854.       printf("WARNING! Can not setuid to %d!\n",switch_uid);
  1855.   }
  1856.  
  1857.   /* Final check */
  1858.   {
  1859.     uid_t userid=getuid();
  1860.     if (!userid) {              /* running as r00t */
  1861.       printf("WARNING! You are running this program as root!\n");
  1862.       printf("It might be a good idea to use the -%%U option to change the userid:\n");
  1863.       printf("Example: -%%U smith\n\n");
  1864.     }
  1865.   }
  1866. #endif
  1867. #endif
  1868.   
  1869.   //printf("WARNING! This is *only* a beta-release of HTTrack\n");
  1870.   io_flush;
  1871.   
  1872. #if DEBUG_STEPS
  1873.   printf("Cache & log settings\n");
  1874. #endif
  1875.   
  1876.   // on utilise le cache..
  1877.   // en cas de prΘsence des deux versions, garder la version la plus avancΘe,
  1878.   // cad la version contenant le plus de fichiers  
  1879.   if (opt->cache) {
  1880.     if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"))) {   // problemes..
  1881.       if ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")) ) { 
  1882.         if ( fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")) ) {
  1883.           if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"))<32768) {
  1884.             if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"))>65536) {
  1885.               if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip")) > fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"))) {
  1886.                 remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"));
  1887.                 rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.zip"), fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.zip"));
  1888.               }
  1889.             }
  1890.           }
  1891.         }
  1892.       }
  1893.       else if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat")) && fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"))) { 
  1894.         if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat")) && fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"))) {
  1895.           // switcher si new<32Ko et old>65Ko (tailles arbitraires) ?
  1896.           // ce cas est peut Ωtre une erreur ou un crash d'un miroir ancien, prendre
  1897.           // alors l'ancien cache
  1898.           if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"))<32768) {
  1899.             if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"))>65536) {
  1900.               if (fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat")) > fsize(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"))) {
  1901.                 remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"));
  1902.                 remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));
  1903.                 rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.dat"),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.dat"));
  1904.                 rename(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/old.ndx"),fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/new.ndx"));  
  1905.                 //} else {  // ne rien faire
  1906.                 //  remove("hts-cache/old.dat");
  1907.                 //  remove("hts-cache/old.ndx");
  1908.               }
  1909.             }
  1910.           }
  1911.         }
  1912.       }
  1913.     }
  1914.   }
  1915.  
  1916.   // DΘbuggage des en tΩtes
  1917.   if (_DEBUG_HEAD) {
  1918.     ioinfo=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-ioinfo.txt"),"wb");
  1919.   }
  1920.   
  1921.   {
  1922.     char n_lock[256];
  1923.     // on peut pas avoir un affichage ET un fichier log
  1924.     // ca sera pour la version 2
  1925.     if (httrack_logmode==1) {
  1926.       opt->log=stdout;
  1927.       opt->errlog=stderr;
  1928.     } else if (httrack_logmode>=2) {
  1929.       // deux fichiers log
  1930.       structcheck(StringBuff(opt->path_log));
  1931.       if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt")))
  1932.         remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt"));
  1933.       if (fexist(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt")))
  1934.         remove(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt"));
  1935.  
  1936.       /* Check FS directory structure created */
  1937.       structcheck(StringBuff(opt->path_log));
  1938.  
  1939.       opt->log=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt"),"w");
  1940.       if (httrack_logmode==2)
  1941.         opt->errlog=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt"),"w");
  1942.       else
  1943.         opt->errlog=opt->log;
  1944.       if (opt->log==NULL) {
  1945.         char s[HTS_CDLMAXSIZE];
  1946.         sprintf(s,"Unable to create log file %s",fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-log.txt"));
  1947.         HTS_PANIC_PRINTF(s);
  1948.         htsmain_free();
  1949.         return -1;
  1950.       } else if (opt->errlog==NULL) {
  1951.         char s[HTS_CDLMAXSIZE];
  1952.         sprintf(s,"Unable to create log file %s",fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-err.txt"));
  1953.         HTS_PANIC_PRINTF(s);
  1954.         htsmain_free();
  1955.         return -1;
  1956.       }
  1957.  
  1958.     } else {
  1959.       opt->log=NULL;
  1960.       opt->errlog=NULL;
  1961.     }
  1962.     
  1963.     // un petit lock-file pour indiquer un miroir en cours, ainsi qu'un Θventuel fichier log
  1964.     {
  1965.       FILE* fp=NULL;
  1966.       //int n=0;
  1967.       char t[256];
  1968.       time_local_rfc822(t);    // faut bien que ca serve quelque part l'heure RFC1945 arf'
  1969.       
  1970.       /* readme for information purpose */
  1971.       {
  1972.         FILE* fp=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/readme.txt"),"wb");
  1973.         if (fp) {
  1974.           fprintf(fp,"What's in this folder?"LF);
  1975.           fprintf(fp,""LF);
  1976.           fprintf(fp,"This folder (hts-cache) has been generated by WinHTTrack "HTTRACK_VERSION"%s"LF, hts_get_version_info(opt));
  1977.           fprintf(fp,"and is used for updating this website."LF);
  1978.           fprintf(fp,"(The HTML website structure is stored here to allow fast updates)"LF""LF);
  1979.           fprintf(fp,"DO NOT delete this folder unless you do not want to update the mirror in the future!!"LF);
  1980.           fprintf(fp,"(you can safely delete old.zip and old.lst files, however)"LF);
  1981.           fprintf(fp,""LF);
  1982.           fprintf(fp,HTS_LOG_SECURITY_WARNING);
  1983.           fclose(fp);
  1984.         }
  1985.       }
  1986.  
  1987.       sprintf(n_lock,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"));
  1988.       //sprintf(n_lock,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"),n);
  1989.       /*do {
  1990.         if (!n)
  1991.           sprintf(n_lock,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress.lock"),n);
  1992.         else
  1993.           sprintf(n_lock,fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-in_progress%d.lock"),n);
  1994.         n++;
  1995.       } while((fexist(n_lock)) && opt->quiet);      
  1996.       if (fexist(n_lock)) {
  1997.         if (!recuperer) {
  1998.           remove(n_lock);
  1999.         }
  2000.       }*/
  2001.  
  2002.       // vΘrifier existence de la structure
  2003.       structcheck(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_html), "/"));
  2004.       structcheck(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log), "/"));
  2005.      
  2006.       // reprise/update
  2007.       if (opt->cache) {
  2008.         FILE* fp;
  2009.         int i;
  2010. #ifdef _WIN32
  2011.         mkdir(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache"));
  2012. #else
  2013.         mkdir(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache"),HTS_PROTECT_FOLDER);
  2014. #endif
  2015.         fp=fopen(fconcat(OPT_GET_BUFF(opt), StringBuff(opt->path_log),"hts-cache/doit.log"),"wb");
  2016.         if (fp) {
  2017.           for(i=0+1;i<argc;i++) {
  2018.             if ( ((strchr(argv[i],' ')!=NULL) || (strchr(argv[i],'"')!=NULL) || (strchr(argv[i],'\\')!=NULL)) && (argv[i][0]!='"')  ) {
  2019.               int j;
  2020.               fprintf(fp,"\"");
  2021.               for(j=0;j<(int) strlen(argv[i]);j++) {
  2022.                 if (argv[i][j]==34)
  2023.                   fprintf(fp,"\\\"");
  2024.                 else if (argv[i][j]=='\\')
  2025.                   fprintf(fp,"\\\\");
  2026.                 else
  2027.                   fprintf(fp,"%c",argv[i][j]);
  2028.               }
  2029.               fprintf(fp,"\"");
  2030.             } else if (strnotempty(argv[i])==0) {   // ""
  2031.               fprintf(fp,"\"\"");
  2032.             } else {   // non critique
  2033.               fprintf(fp,"%s",argv[i]);
  2034.             }
  2035.             if (i<argc-1)
  2036.               fprintf(fp," ");
  2037.           }
  2038.           fprintf(fp,LF);
  2039.           fprintf(fp,"File generated automatically on %s, do NOT edit"LF,t);
  2040.           fprintf(fp,LF);
  2041.           fprintf(fp,"To update a mirror, just launch httrack without any parameters"LF);
  2042.           fprintf(fp,"The existing cache will be used (and modified)"LF);
  2043.           fprintf(fp,"To have other options, retype all parameters and launch HTTrack"LF);
  2044.           fprintf(fp,"To continue an interrupted mirror, just launch httrack without any parameters"LF);
  2045.           fprintf(fp,LF);
  2046.           fclose(fp); fp=NULL;
  2047.         //} else if (opt->debug>1) {
  2048.         //  printf("! FileOpen error, \"%s\"\n",strerror(errno));
  2049.         }
  2050.       }
  2051.       
  2052.       // petit message dans le lock
  2053.       if ( (fp=fopen(n_lock,"wb"))!=NULL) {
  2054.         int i;
  2055.         fprintf(fp,"Mirror in progress since %s .. please wait!"LF,t);
  2056.         for(i=0;i<argc;i++) {
  2057.           if (strchr(argv[i],' ')==NULL)
  2058.             fprintf(fp,"%s ",argv[i]);
  2059.           else    // entre ""
  2060.             fprintf(fp,"\"%s\" ",argv[i]);
  2061.         }
  2062.         fprintf(fp,LF);
  2063.         fprintf(fp, "To pause the engine: create an empty file named 'hts-stop.lock'"LF);
  2064. #if USE_BEGINTHREAD
  2065.                 fprintf(fp, "PID=%d\n", (int)getpid());
  2066. #ifndef _WIN32
  2067.                 fprintf(fp, "UID=%d\n", (int)getuid());
  2068.                 fprintf(fp, "GID=%d\n", (int)getuid());
  2069. #endif
  2070.                 fprintf(fp, "START=%d\n", (int)time(NULL));
  2071. #endif
  2072.         fclose(fp); fp=NULL;
  2073.       }
  2074.       
  2075.       // fichier log        
  2076.       if (opt->log)     {
  2077.         int i;
  2078.         fprintf(opt->log,"HTTrack"HTTRACK_VERSION"%s launched on %s at %s"LF, 
  2079.           hts_get_version_info(opt),
  2080.           t, url);
  2081.         fprintf(opt->log,"(");
  2082.         for(i=0;i<argc;i++) {
  2083.           if (strchr(argv[i],' ') == NULL || strchr(argv[i],'\"') != NULL)
  2084.             fprintf(opt->log,"%s ",argv[i]);
  2085.           else    // entre "" (si espace(s) et pas dΘja de ")
  2086.             fprintf(opt->log,"\"%s\" ",argv[i]);
  2087.         }
  2088.         fprintf(opt->log,")"LF);
  2089.         fprintf(opt->log,LF);
  2090.         fprintf(opt->log,"Information, Warnings and Errors reported for this mirror:"LF);
  2091.         fprintf(opt->log,HTS_LOG_SECURITY_WARNING );
  2092.         fprintf(opt->log,LF);
  2093.       }
  2094.  
  2095.       if (httrack_logmode) {
  2096.         printf("Mirror launched on %s by HTTrack Website Copier/"HTTRACK_VERSION"%s "HTTRACK_AFF_AUTHORS""LF,t,hts_get_version_info(opt));
  2097.         if (opt->wizard==0) {
  2098.           printf("mirroring %s with %d levels, %d sockets,t=%d,s=%d,logm=%d,lnk=%d,mdg=%d\n",url,opt->depth,opt->maxsoc,opt->travel,opt->seeker,httrack_logmode,opt->urlmode,opt->getmode);
  2099.         } else {    // the magic wizard
  2100.           printf("mirroring %s with the wizard help..\n",url);
  2101.         }
  2102.       }
  2103.     }
  2104.     
  2105.     io_flush;
  2106.  
  2107.     /* Enforce limits to avoid bandwith abuse. The bypass_limits should only be used by administrators and experts. */
  2108.     if (!opt->bypass_limits) {
  2109.       if (opt->maxsoc <= 0 || opt->maxsoc > 4) {
  2110.         opt->maxsoc = 4;
  2111.         if (opt->log != NULL) {
  2112.           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum number of simultaneous connections limited to %d to avoid server overload"LF, (int)opt->maxsoc);
  2113.         }
  2114.       }
  2115.       if (opt->maxrate <= 0 || opt->maxrate > 100000) {
  2116.         opt->maxrate = 100000;
  2117.         if (opt->log != NULL) {
  2118.           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum bandwidth limited to %d to avoid server overload"LF, (int)opt->maxrate);
  2119.         }
  2120.       }
  2121.       if (opt->maxconn <= 0 || opt->maxconn > 5.0) {
  2122.         opt->maxconn = 5.0;
  2123.         if (opt->log != NULL) {
  2124.           HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: maximum number of connections per second limited to %f to avoid server overload"LF, (float)opt->maxconn);
  2125.         }
  2126.       }
  2127.     } else {
  2128.       if (opt->log != NULL) {
  2129.         HTS_LOG(opt,LOG_WARNING); fprintf(opt->log,"* security warning: !!! BYPASSING SECURITY LIMITS - MONITOR THIS SESSION WITH EXTREME CARE !!!"LF);
  2130.       }
  2131.     }
  2132.  
  2133.   /* Info for wrappers */
  2134.   if ( (opt->debug>0) && (opt->log!=NULL) ) {
  2135.     HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: init"LF);
  2136.   }
  2137.  
  2138.   /* Init external */
  2139.   RUN_CALLBACK_NOARG(opt, init);
  2140.  
  2141.   // dΘtourner SIGHUP etc.
  2142. #if DEBUG_STEPS
  2143.   printf("Launching the mirror\n");
  2144. #endif
  2145.  
  2146.     // Lancement du miroir
  2147.     // ------------------------------------------------------------
  2148.     if (httpmirror(url, opt)==0) {
  2149.       printf("Error during operation (see log file), site has not been successfully mirrored\n");
  2150.     } else {
  2151.       if  (opt->shell) {
  2152.         HTT_REQUEST_START;
  2153.         HT_PRINT("TRANSFER DONE"LF);
  2154.         HTT_REQUEST_END
  2155.       } else {
  2156.         printf("Done.\n");
  2157.       }
  2158.     }
  2159.     // ------------------------------------------------------------
  2160.  
  2161.     //
  2162.     // Build top index
  2163.     if (opt->dir_topindex) {
  2164.       char BIGSTK rpath[1024*2];
  2165.       char* a;
  2166.       strcpybuff(rpath,StringBuff(opt->path_html));
  2167.       if (rpath[0]) {
  2168.         if (rpath[strlen(rpath)-1]=='/')
  2169.           rpath[strlen(rpath)-1]='\0';
  2170.       }
  2171.       a=strrchr(rpath,'/');
  2172.       if (a) {
  2173.         *a='\0';
  2174.         hts_buildtopindex(opt,rpath,StringBuff(opt->path_bin));
  2175.         if (opt->log) {
  2176.           HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"Top index rebuilt (done)"LF);
  2177.         }
  2178.       }
  2179.     }
  2180.  
  2181.     if (opt->state.exit_xh ==1) {
  2182.       if (opt->log) {
  2183.         fprintf(opt->log,"* * MIRROR ABORTED! * *\nThe current temporary cache is required for any update operation and only contains data downloaded during the present aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files]\n");
  2184.       }
  2185.     }
  2186.  
  2187.     /* Info for wrappers */
  2188.     if ( (opt->debug>0) && (opt->log!=NULL) ) {
  2189.       HTS_LOG(opt,LOG_INFO); fprintf(opt->log,"engine: free"LF);
  2190.     }
  2191.  
  2192.     /* UnInit */
  2193.     RUN_CALLBACK_NOARG(opt, uninit);
  2194.  
  2195.     if (httrack_logmode!=1) {
  2196.       if (opt->errlog == opt->log) opt->errlog=NULL;
  2197.       if (opt->log) { fclose(opt->log); opt->log=NULL; }
  2198.       if (opt->errlog) { fclose(opt->errlog); opt->errlog=NULL; }
  2199.     }  
  2200.  
  2201.     // DΘbuggage des en tΩtes
  2202.     if (_DEBUG_HEAD) {
  2203.       if (ioinfo) {
  2204.         fclose(ioinfo);
  2205.       }
  2206.     }
  2207.  
  2208.     // supprimer lock
  2209.     remove(n_lock);
  2210.   }
  2211.  
  2212.   if (x_argvblk)
  2213.     freet(x_argvblk);
  2214.   if (x_argv)
  2215.     freet(x_argv);
  2216.   if (url)
  2217.     freet(url);
  2218.  
  2219. #ifdef HTS_TRACE_MALLOC
  2220.   hts_freeall();
  2221. #endif
  2222.  
  2223.   printf("Thanks for using HTTrack!\n");
  2224.   io_flush;
  2225.   htsmain_free();
  2226.   return 0;    // OK
  2227. }
  2228.  
  2229.  
  2230. // main() subroutines
  2231.  
  2232. // vΘrifier chemin path
  2233. int check_path(String* s, char* defaultname) {
  2234.   int i;
  2235.   int return_value=0;
  2236.  
  2237.   // Replace name: ~/mywebsites/# -> /home/foo/mywebsites/#
  2238.   expand_home(s);
  2239.   for(i = 0 ; i < (int) StringLength(*s) ; i++)    // conversion \ -> /
  2240.     if (StringSub(*s, i) == '\\')
  2241.       StringSubRW(*s, i) = '/';
  2242.   
  2243.   // remove ending /
  2244.   if (StringNotEmpty(*s) && StringRight(*s, 1) == '/')
  2245.     StringPopRight(*s);
  2246.  
  2247.    // Replace name: /home/foo/mywebsites/# -> /home/foo/mywebsites/wonderfulsite
  2248.   if (StringNotEmpty(*s)) {
  2249.     if (StringRight(*s, 1) == '#') {
  2250.       if (strnotempty((defaultname?defaultname:""))) {
  2251.         char* a = strchr(defaultname,'#');      // we never know..
  2252.         if (a)
  2253.           *a='\0';
  2254.         StringPopRight(*s);
  2255.         StringCat(*s, defaultname);
  2256.       } else {
  2257.         StringClear(*s); // Clear path (no name/default url given)
  2258.       }
  2259.       return_value=1;     // expanded
  2260.     }
  2261.   }
  2262.  
  2263.   // ending /
  2264.   if (StringNotEmpty(*s) && StringRight(*s, 1) != '/')    // ajouter slash α la fin
  2265.     StringCat(*s, "/");
  2266.  
  2267.   return return_value;
  2268. }
  2269.  
  2270. // dΘtermine si l'argument est une option
  2271. int cmdl_opt(char* s) {
  2272.   if (s[0]=='-') {  // c'est peut Ωtre une option
  2273.     if (strchr(s,'.')!=NULL && strchr(s,'%')==NULL)
  2274.       return 0;    // sans doute un -www.truc.fr (note: -www n'est pas compris)
  2275.     else if (strchr(s,'/')!=NULL)
  2276.       return 0;    // idem, -*cgi-bin/
  2277.     else if (strchr(s,'*')!=NULL)
  2278.       return 0;    // joker, idem
  2279.     else
  2280.       return 1;
  2281.   } else return 0;
  2282. }
  2283.  
  2284.